﻿Imports TMySQL
Public Class frmPrivileges
    Implements ITabForm
    Private _UserList As TMyUserList
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Public Sub New(ByVal AUserList As TMyUserList)
        Me.New()
        _UserList = AUserList
    End Sub
    Private Sub InitFrm()
        _UserList.Refresh()
        tvUsers.Nodes.Clear()
        Dim nodeR As TreeNode = tvUsers.Nodes.Add(frmMain.DatabaseList.HostName)
        For i As Integer = 0 To _UserList.Count - 1
            Dim node As TreeNode = nodeR.Nodes.Add(_UserList(i).UserName & "@" & _UserList(i).Host)
            node.Tag = _UserList(i)
        Next
        tvUsers.ExpandAll()
    End Sub

    Private Sub frmPrivs_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitFrm()
    End Sub

    Private Sub tvUsers_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvUsers.AfterSelect
        pgc.SuspendLayout()
        tbsGlobal.SuspendLayout()
        tbsDatabase.SuspendLayout()
        tbsTable.SuspendLayout()
        tbsColumn.SuspendLayout()
        tbsRoutine.SuspendLayout()
        Try
            InitTabSheet()
            If e.Node.Level <= 0 Then
                Return
            End If
            ShowPrivs(e.Node.Tag)
        Finally
            pgc.ResumeLayout()
            tbsGlobal.ResumeLayout()
            tbsDatabase.ResumeLayout()
            tbsTable.ResumeLayout()
            tbsColumn.ResumeLayout()
            tbsRoutine.ResumeLayout()
        End Try
    End Sub

    Private Sub InitTabSheet()
        tbsGlobal.Controls.Clear()
        tbsDatabase.Controls.Clear()
        tbsTable.Controls.Clear()
        tbsColumn.Controls.Clear()
        tbsRoutine.Controls.Clear()
    End Sub

    Private Sub ShowPrivs(ByVal AUser As TMyUser)
        AUser.Refresh()
        ShowGlobalPrivs(AUser.GlobalPrivs)
        ShowDatabasePrivs(AUser.DatabasePrivs)
        ShowTablePrivs(AUser.TablePrivs)
        ShowColumnPrivs(AUser.ColumnPrivs)
        ShowRoutinePrivs(AUser.ProcPrivs)
    End Sub

    Private Sub ShowGlobalPrivs(ByVal APrivs As TMyGlobalPrivileges)
        If tbsGlobal.Controls.Count > 0 Then
            tbsGlobal.Controls.Clear()
        End If
        Dim frmRet As New frmGlobalPrivileges(APrivs)
        frmRet.TopLevel = False
        frmRet.Parent = tbsGlobal
        frmRet.Dock = DockStyle.Fill
        frmRet.Show()
    End Sub

    Private Sub ShowDatabasePrivs(ByVal APrivs As TMyDatabasePrivilegesList)
        If tbsDatabase.Controls.Count > 0 Then
            tbsGlobal.Controls.Clear()
        End If
        Dim frmRet As New frmDatabasePrivileges(APrivs)
        frmRet.TopLevel = False
        frmRet.Parent = tbsDatabase
        frmRet.Dock = DockStyle.Fill
        frmRet.Show()
    End Sub

    Private Sub ShowTablePrivs(ByVal APrivs As TMyTablePrivilegesList)
        If tbsTable.Controls.Count > 0 Then
            tbsGlobal.Controls.Clear()
        End If
        Dim frmRet As New frmTablePrivileges(APrivs)
        frmRet.TopLevel = False
        frmRet.Parent = tbsTable
        frmRet.Dock = DockStyle.Fill
        frmRet.Show()
    End Sub
    Private Sub ShowColumnPrivs(ByVal APrivs As TMyColumnPrivilegesList)
        If tbsColumn.Controls.Count > 0 Then
            tbsGlobal.Controls.Clear()
        End If
        Dim frmRet As New frmColumnPrivileges(APrivs)
        frmRet.TopLevel = False
        frmRet.Parent = tbsColumn
        frmRet.Dock = DockStyle.Fill
        frmRet.Show()
    End Sub

    Private Sub ShowRoutinePrivs(ByVal APrivs As TMyProcPrivilegesList)
        If tbsRoutine.Controls.Count > 0 Then
            tbsGlobal.Controls.Clear()
        End If
        Dim frmRet As New frmProcPrivileges(APrivs)
        frmRet.TopLevel = False
        frmRet.Parent = tbsRoutine
        frmRet.Dock = DockStyle.Fill
        frmRet.Show()
    End Sub

    Private Sub mnuAddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAddUser.Click
        Dim frmRet As New frmCreateUser
        Try
            frmRet.ShowDialog()
            InitFrm()
        Catch ex As Exception
            frmRet.Dispose()
        End Try

    End Sub

    Private Sub ContextMenuStrip1_Opened(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenuStrip1.Opened
        mnuDeleteUser.Enabled = tvUsers.SelectedNode.Level.Equals(1)
    End Sub

    Private Sub mnuDeleteUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDeleteUser.Click
        If tvUsers.SelectedNode.Level <= 0 Then
            Return
        End If
        If MsgBox("ユーザー " & tvUsers.SelectedNode.Text & "を削除しますか？", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) <> MsgBoxResult.Yes Then
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            DeleteUser(tvUsers.SelectedNode.Text, tvUsers.SelectedNode.Parent.Text)
            Call MsgBox("ユーザーを削除しました", MsgBoxStyle.Information)
            InitFrm()
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub
    Private Sub DeleteUser(ByVal AUserName As String, ByVal AHostName As String)
        Dim myQuery As New TMyQuery
        myQuery.Session = frmMain.Connection
        myQuery.Clear()
        myQuery.SQL.Append("DROP USER '").Append(AUserName).Append("'@'").Append(AHostName).Append("'")
        myQuery.Execute()
    End Sub
End Class